<?php
/**
 * 会员中心--收藏功能
 *
 * @copyright  Copyright (c) 2007-2013 ShopNC Inc. (http://www.shopnc.net)
 * @license    http://www.shopnc.net
 * @link       http://www.shopnc.net
 * @since      File available since Release v1.1
 */
defined('InShopNC') or exit('Access Invalid!');

class member_favoritesControl extends MobileControl
{
    public function __construct()
    {
        parent::__construct();
    }

    //我的收藏
    public function indexOp()
    {

        Tpl::showpage('myfavs');
    }

    /**
     * 增加商品收藏
     */
    public function favoritesgoodsOp()
    {
        $fav_id = intval($_GET['fid']);
        if ($fav_id <= 0) {
            echo json_encode(array('done' => false, 'msg' => '收藏失败'));
            die;
        }
        $favorites_model = new favoritesModel();
        //判断是否已经收藏
        $favorites_info = $favorites_model->getOneFavorites(array('fav_id' => "$fav_id", 'fav_type' => 'goods', 'member_id' => "{$_SESSION['member_id']}"));
        if (!empty($favorites_info)) {
            echo json_encode(array('done' => false, 'msg' => '您已收藏过该商品'));
            die;
        }
        //判断商品是否为当前会员所有
        $goods_model = new goodsModel();
        $goods_info = $goods_model->getOne($fav_id);
        if ($goods_info['store_id'] == $_SESSION['store_id']) {
            echo json_encode(array('done' => false, 'msg' => '不能收藏自己的商品'));
            die;
        }
        //添加收藏
        $insert_arr = array();
        $insert_arr['member_id'] = $_SESSION['member_id'];
        $insert_arr['fav_id'] = $fav_id;
        $insert_arr['fav_type'] = 'goods';
        $insert_arr['fav_time'] = time();
        $result = $favorites_model->addFavorites($insert_arr);
        if ($result) {
            //增加收藏数量
            $goods_model->updateGoodsAllUser(array('goods_collect' => array('sign' => 'increase', 'value' => '1')), $fav_id);
            //***********更新商品权重***************
            goodsModel::updateGoodsRank($goods_info['goods_id']);
            //**************************
            echo json_encode(array('done' => true, 'msg' => '收藏成功'));
            die;
        } else {
            echo json_encode(array('done' => false, 'msg' => '收藏失败'));
            die;
        }
    }

    /**
     * 增加商城收藏
     */
    public function favoritesstoreOp()
    {
        $fav_id = intval($_GET['fid']);
        if ($fav_id <= 0) {
            echo json_encode(array('done' => false, 'msg' => '收藏失败'));
            die;
        }
        $favorites_model = new favoritesModel();
        //判断是否已经收藏
        $favorites_info = $favorites_model->getOneFavorites(array('fav_id' => "$fav_id", 'fav_type' => 'store', 'member_id' => "{$_SESSION['member_id']}"));
        if (!empty($favorites_info)) {
            echo json_encode(array('done' => false, 'msg' => '您已收藏过该商城'));
            die;
        }
        //判断商城是否为当前会员所有
        if ($fav_id == $_SESSION['store_id']) {
            echo json_encode(array('done' => false, 'msg' => '不能收藏自己的商城'));
            die;
        }
        //添加收藏
        $insert_arr = array();
        $insert_arr['member_id'] = $_SESSION['member_id'];
        $insert_arr['fav_id'] = $fav_id;
        $insert_arr['fav_type'] = 'store';
        $insert_arr['fav_time'] = time();
        $result = $favorites_model->addFavorites($insert_arr);
        if ($result) {
            //增加收藏数量
            $store_model = new storeModel();
            $store_model->storeUpdate(array('store_id' => $fav_id, 'store_collect' => array('sign' => 'increase', 'value' => '1')), $fav_id);
            echo json_encode(array('done' => true, 'msg' => '收藏成功'));
            die;
        } else {
            echo json_encode(array('done' => false, 'msg' => '收藏失败'));
            die;
        }
    }

    /**
     * 增加品牌收藏
     */
    public function favoritesbrandOp()
    {
        $fav_id = intval($_GET['fid']);
        if ($fav_id <= 0) {
            echo json_encode(array('done' => false, 'msg' => '收藏失败'));
            die;
        }
        //判断是否已经收藏
        $sql = "SELECT * FROM #__brand_favorites WHERE member_id = '{$_SESSION['member_id']}' AND brand_fav_id = '$fav_id'";
        $favorites_info = Db::queryRow($sql);
        if (!empty($favorites_info)) {
            echo json_encode(array('done' => false, 'msg' => '您已收藏过该品牌'));
            die;
        }
        //判断品牌是否为当前会员所有
        $sql = "SELECT brand_store_id FROM #__brand WHERE brand_id = '$fav_id'";
        $brand_info = Db::queryRow($sql);
        if ($brand_info['brand_store_id'] == $_SESSION['brand_store_id']) {
            echo json_encode(array('done' => false, 'msg' => '不能收藏自己的品牌'));
            die;
        }
        //添加收藏
        $insert_arr = array();
        $insert_arr['member_id'] = $_SESSION['member_id'];
        $insert_arr['brand_fav_id'] = $fav_id;
        $insert_arr['brand_fav_time'] = time();
        $result = Db::insert('brand_favorites', $insert_arr);
        if ($result) {
            //增加收藏数量
            Db::update('brand', array('brand_collect' => array('sign' => 'increase', 'value' => '1')), "brand_id = '$fav_id'");
            echo json_encode(array('done' => true, 'msg' => '收藏成功'));
            die;
        } else {
            echo json_encode(array('done' => false, 'msg' => '收藏失败'));
            die;
        }
    }

    /**
     * 商品收藏列表
     *
     * @param
     * @return
     */
    public function fglistOp()
    {
        $favorites_model = new favoritesModel();
        $page = new Page();
        $page->setEachNum(10);
        $page->setStyle('admin');
        $favorites_list = $favorites_model->getFavoritesList(array('member_id' => "{$_SESSION['member_id']}", 'fav_type' => 'goods'), $page);

        if (!empty($favorites_list) && is_array($favorites_list)) {
            $favorites_id = array();//收藏的商品编号
            foreach ($favorites_list as $key => $favorites) {
                $favorites_id[] = $favorites['fav_id'];
            }
            $goods_model = new goodsModel();
            $goods_list = $goods_model->getGoods(array('goods_id_in' => "'" . implode("','", $favorites_id) . "'", 'goods_state' => '0'), '',
                'goods.goods_id,goods.goods_name,goods.store_id,goods.goods_image,goods.goods_store_price,goods.commentnum,goods.salenum,goods.goods_collect', 'goods');
            /*    echo "<pre>";
                var_dump($goods_list);exit;*/
            if (!empty($goods_list) && is_array($goods_list)) {
                echo json_encode(array('state' => true, 'goods_list' => $goods_list));
                exit;
            } else {
                echo json_encode(array('state' => false));
                exit;
            }
        } else {
            echo json_encode(array('state' => false));
            exit;
        }
    }

    /**
     * 商城收藏列表
     * @param
     * @return
     */
    public function fslistOp()
    {
        $favorites_model = new favoritesModel();
        $page = new Page();
        $page->setEachNum(10);
        $favorites_list = $favorites_model->getFavoritesList(array('member_id' => "{$_SESSION['member_id']}", 'fav_type' => 'store'), $page);
        if (!empty($favorites_list) && is_array($favorites_list)) {
            $favorites_id = array();//收藏的商城编号
            foreach ($favorites_list as $key => $favorites) {
                $favorites_id[] = $favorites['fav_id'];
            }
            $store_model = new storeModel();
            $store_list = $store_model->getStoreList(array('store_id_in' => "'" . implode("','", $favorites_id) . "'"));

            if (!empty($store_list) && is_array($store_list)) {
                echo json_encode(array('state' => true, 'store_list' => $store_list));
                exit;
            } else {
                echo json_encode(array('state' => false));
                exit;
            }
        } else {
            echo json_encode(array('state' => false));
            exit;
        }
    }

    /**
     * 品牌收藏列表
     *
     * @param
     * @return
     */
    public function fblistOp()
    {
        $model = Model();
        $page = new Page();
        $page->setEachNum(10);
        $page->setStyle('admin');

        $param['table'] = 'brand_favorites';
        $param['where'] = "member_id = '{$_SESSION['member_id']}'";
        $param['order'] = 'brand_fav_time desc';
        $favorites_list = Db::select($param, $page);

        if (!empty($favorites_list) && is_array($favorites_list)) {
            $favorites_id = array();//收藏的品牌编号
            foreach ($favorites_list as $key => $favorites) {
                $favorites_id[] = $favorites['brand_fav_id'];
            }
            $ids = implode(',', $favorites_id);
            $brand_list = $model->table('brand')->where("brand_id in ($ids)")->select();   //查找品牌商城信息
            /*echo "<pre >";
            var_dump($brand_list);exit;*/
            if (!empty($brand_list) && is_array($brand_list)) {
                echo json_encode(array('state' => true, 'brand_list' => $brand_list));
                exit;
            } else {
                echo json_encode(array('state' => false));
                exit;
            }
        } else {
            echo json_encode(array('state' => false));
            exit;
        }
    }

    /**
     * 删除收藏
     *
     * @param
     * @return
     */
    public function delfavoritesOp()
    {
        if (!$_GET['fav_id'] || !$_GET['type']) {
            showDialog(Language::get('member_favorite_del_fail'), '', 'error');
        }
        $fav_id = $_GET['fav_id'];
        $type = $_GET['type'];
        $favorites_model = new favoritesModel();
        $fav_arr = $fav_list = explode(',', $_GET['fav_id']);
        if (!empty($fav_arr) && is_array($fav_arr)) {
            //批量删除
            $fav_str = "'" . implode("','", $fav_arr) . "'";
            $result = $favorites_model->delFavorites(array('fav_id_in' => "$fav_str", 'fav_type' => "$type", 'member_id' => "{$_SESSION['member_id']}"));
            if ($result) {
                //剔除删除失败的记录
                $favorites_list = $favorites_model->getFavoritesList(array('fav_id_in' => "$fav_str", 'fav_type' => "$type", 'member_id' => "{$_SESSION['member_id']}"));
                if (!empty($favorites_list)) {
                    foreach ($favorites_list as $k => $v) {
                        unset($fav_arr[array_search($v['fav_id'], $fav_arr)]);
                    }
                }
                if (!empty($fav_arr)) {
                    if ($type == 'goods') {
                        $fav_str = "'" . implode("','", $fav_arr) . "'";
                        //更新收藏数量
                        $goods_model = new goodsModel();
                        $goods_model->updateGoods(array('goods_collect' => array('sign' => 'decrease', 'value' => '1')), $fav_arr);
                        //***********更新商品权重***************
                        foreach ($fav_list as $fav_id) {
                            goodsModel::updateGoodsRank($fav_id);
                        }
                        //*************************
                        showDialog(Language::get('favorite_del_success'), 'index.php?act=member_favorites&op=fglist&show=' . $_GET['show'], 'succ');
                    } else {
                        $fav_str = "'" . implode("','", $fav_arr) . "'";
                        //更新收藏数量
                        $store_model = new storeModel();
                        $store_model->updateByCondtion(array('store_collect' => array('sign' => 'decrease', 'value' => '1')), array('store_id_in' => $fav_str));
                        showDialog(Language::get('favorite_del_success'), 'index.php?act=member_favorites&op=fslist', 'succ');
                    }
                }
            } else {
                showDialog(Language::get('favorite_del_fail'), '', 'error');
            }

        } else {
            showDialog(Language::get('member_favorite_del_fail'), '', 'error');
        }
    }

    /**
     * Ajax取消收藏
     *
     * @param
     * @return
     */
    public function ajaxDelFavoritesOp()
    {
        if (!$_GET['fav_id'] || !$_GET['type']) {
            echo json_encode(array('done' => false, 'msg' => '取消收藏失败'));
            exit;
        }
        $fav_id = (int)$_GET['fav_id'];
        $type = $_GET['type'];
        $favorites_model = new favoritesModel();
        $result = $favorites_model->delFavorites(array('fav_id' => "$fav_id", 'fav_type' => "$type", 'member_id' => "{$_SESSION['member_id']}"));
        if ($result) {
            echo json_encode(array('done' => true, 'msg' => '取消收藏成功'));
            if ($type == 'goods') {
                //更新收藏数量
                $goods_model = new goodsModel();
                $goods_model->updateGoods(array('goods_collect' => array('sign' => 'decrease', 'value' => '1')), array($fav_id));
                //***********更新商品权重***************
                goodsModel::updateGoodsRank($fav_id);
            } else {
                //更新收藏数量
                $store_model = new storeModel();
                $store_model->updateByCondtion(array('store_collect' => array('sign' => 'decrease', 'value' => '1')), array('store_id' => $fav_id));
            }

        } else {
            echo json_encode(array('done' => false, 'msg' => '取消收藏失败'));
            exit;
        }
    }

    /**
     * 删除品牌收藏
     *
     * @param
     * @return
     */
    public function delfavoritesbrandOp()
    {
        if (!$_GET['fav_id'] || !(int)$_GET['fav_id'] > 0) {
            echo json_encode(array('done' => false, 'msg' => '取消收藏失败'));
            exit;
        }
        $fav_id = (int)$_GET['fav_id'];
        $result = model()->table('brand_favorites')->where(array('brand_fav_id' => "$fav_id", 'member_id' => "{$_SESSION['member_id']}"))->delete();
        if ($result) {
            echo json_encode(array('done' => true, 'msg' => '取消收藏成功'));
            Db::update('brand', array('brand_collect' => array('sign' => 'decrease', 'value' => '1')), "brand_id = '$fav_id'");
        } else {
            echo json_encode(array('done' => false, 'msg' => '取消收藏失败'));
            exit;
        }

    }

    /*
     * 会员最近浏览商品列表
     * */
    public function viewed_listOp()
    {
        if (cookie('viewed_goods')) {
            $string_viewed_goods = cookie('viewed_goods');
            if (get_magic_quotes_gpc()) {
                $string_viewed_goods = stripslashes($string_viewed_goods); // 去除斜杠
            }
            //反转数组，使得最后浏览在前面
            $cookie_array = array_reverse(unserialize($string_viewed_goods));
            $goods_id_in = '';
            foreach (( array )$cookie_array as $k => $v) {
                $info = explode("-", $v);
                $goods_id_in .= $info [0] . ',';
            }

            $goods_id_in = rtrim($goods_id_in, ',');
            $model_goods = new goodsModel ();
            $viewed_list = $model_goods->getGoods(array(
                'goods_id_in' => $goods_id_in
            ), '', 'goods_id, goods_name, goods_store_price, goods_image, store_id', 'goods');

        }
        Tpl::output('viewed_goods', $viewed_list);
        Tpl::showpage('viewed_goods');
    }
}
